﻿using MediatR;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.dto;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class GetServiceHandler : IRequestHandler<GetServiceCommand, APIResult<List<ServiceDto>>>
    {
        private readonly IBaseRepository<ServiceAudit> serviceRepo;
        private readonly IBaseRepository<Scheduling> schedRepo;
        private readonly IBaseRepository<Doctor> doctorRepo;

        public GetServiceHandler(IBaseRepository<ServiceAudit> serviceRepo,IBaseRepository<Scheduling> schedRepo,IBaseRepository<Doctor> doctorRepo)
        {
            this.serviceRepo = serviceRepo;
            this.schedRepo = schedRepo;
            this.doctorRepo = doctorRepo;
        }
        /// <summary>
        /// 服务
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<ServiceDto>>> Handle(GetServiceCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<ServiceDto>> res = new APIResult<List<ServiceDto>>();
             var service = serviceRepo.GetAll();
            var sched = schedRepo.GetAll();
            var doctor = doctorRepo.GetAll();
            var linq = from a in service
                       join b in sched on a.SchedulId equals b.SchedulId
                       join c in doctor on b.DoctorId equals c.DoctorId
                       select new ServiceDto
                       {
                           DoctorId =a.DoctorId,
                           JieshuTime = b.JieshuTime,
                           PatientNumber = b.PatientNumber,
                           PriceSetting = a.PriceSetting,
                           Registration = a.Registration,
                           SchedulId = b.SchedulId,
                           ServiceDepict = a.ServiceDepict,
                           SchedulingTime = b.SchedulingTime,
                       };
            res.Code = APIEnum.Success;
            res.Data = linq.ToList();
            return res;
        }
    }
}
